﻿<template>
	<div class="employee-container">
		<el-dialog v-model="isShowDialog" :width="960" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="项目" prop="projectId">
							<el-select clearable filterable v-model="ruleForm.projectId" placeholder="请选择项目">
								<el-option v-for="(item, index) in projectProjectIdDropdownList" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="住建ID" prop="zjEmpID">
							<el-input v-model="ruleForm.zjEmpID" placeholder="请输入住建ID" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="姓名" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入姓名" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="身份证号" prop="idNum">
							<el-input v-model="ruleForm.idNum" placeholder="请输入身份证号" maxlength="30" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="年龄" prop="age">
							<el-input-number v-model="ruleForm.age" placeholder="请输入年龄" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="性别" prop="sex">
							<el-select clearable v-model="ruleForm.sex" placeholder="请选择性别">
								<el-option v-for="(item, index) in dl('GenderEnum')" :key="index" :value="Number(item.code)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="民族" prop="nation">
							<el-input v-model="ruleForm.nation" placeholder="请输入民族" maxlength="10" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="生日" prop="birthday">
							<el-input v-model="ruleForm.birthday" placeholder="请输入生日" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="住址" prop="address">
							<el-input v-model="ruleForm.address" placeholder="请输入住址" maxlength="300" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="户籍" prop="native">
							<el-input v-model="ruleForm.native" placeholder="请输入户籍" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="电话" prop="phone">
							<el-input v-model="ruleForm.phone" placeholder="请输入电话" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="注册人脸" prop="facephoto">
							<el-input v-model="ruleForm.facephoto" rows="4" placeholder="请输入注册人脸" type="textarea" maxlength="1000000" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工种" prop="workType">
							<el-input v-model="ruleForm.workType" placeholder="请输入工种" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="工作组" prop="workGroup">
							<el-input v-model="ruleForm.workGroup" placeholder="请输入工作组" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="紧急联系人" prop="emerPeople">
							<el-input v-model="ruleForm.emerPeople" placeholder="请输入紧急联系人" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="紧急联系人电话" prop="emerPhone">
							<el-input v-model="ruleForm.emerPhone" placeholder="请输入紧急联系人电话" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="文化程度" prop="culture">
							<el-input v-model="ruleForm.culture" placeholder="请输入文化程度" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="健康状况" prop="health">
							<el-input v-model="ruleForm.health" placeholder="请输入健康状况" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="签发机关（长度20个字符）" prop="qianFaJiGuan">
							<el-input v-model="ruleForm.qianFaJiGuan" placeholder="请输入签发机关（长度20个字符）" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="身份证正面照" prop="iDcard_A">
							<el-input v-model="ruleForm.iDcard_A" placeholder="请输入身份证正面照" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="身份证反面照" prop="iDcard_B">
							<el-input v-model="ruleForm.iDcard_B" placeholder="请输入身份证反面照" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="身份证起始日期" prop="qiXianQiShi">
							<el-input v-model="ruleForm.qiXianQiShi" placeholder="请输入身份证起始日期" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="身份证失效日期" prop="qiXianShiXiao">
							<el-input v-model="ruleForm.qiXianShiXiao" placeholder="请输入身份证失效日期" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="银行名称" prop="bankName">
							<el-input v-model="ruleForm.bankName" placeholder="请输入银行名称" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="银行账号" prop="bankAccount">
							<el-input v-model="ruleForm.bankAccount" placeholder="请输入银行账号" maxlength="50" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否自动审核" prop="isAutoAudit">
							<el-switch v-model="ruleForm.isAutoAudit" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否允许移动打卡" prop="isMobile">
							<el-switch v-model="ruleForm.isMobile" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="移动端登录账号" prop="mobileAccount">
							<el-input v-model="ruleForm.mobileAccount" placeholder="请输入移动端登录账号" maxlength="255" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="移动端登录密码" prop="mobilePassword">
							<el-input v-model="ruleForm.mobilePassword" placeholder="请输入移动端登录密码" maxlength="255" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="注册人脸长度" prop="picLen">
							<el-input-number v-model="ruleForm.picLen" placeholder="请输入注册人脸长度" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormRules } from 'element-plus';
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
import { formatDate } from '/@/utils/formatTime';
import { addEmployee, updateEmployee, detailEmployee } from '/@/api/main/employee';
import { getProjectProjectIdDropdown } from '/@/api/main/employee';
import { getAPI } from '/@/utils/axios-utils';
import { SysEnumApi } from '/@/api-services/api';

//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: '',
	},
});
//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
//自行添加其他规则
const rules = ref<FormRules>({
	name: [{ required: true, message: '请输入姓名！', trigger: 'blur' }],
	age: [{ required: true, message: '请输入年龄！', trigger: 'blur' }],
});

// 页面加载时
onMounted(() => {});

// 打开弹窗
const openDialog = async (row: any) => {
	// ruleForm.value = JSON.parse(JSON.stringify(row));
	// 改用detail获取最新数据来编辑
	let rowData = JSON.parse(JSON.stringify(row));
	if (rowData.id) ruleForm.value = (await detailEmployee(rowData.id)).data.result;
	else ruleForm.value = rowData;
	isShowDialog.value = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit('reloadTable');
	isShowDialog.value = false;
};

// 取消
const cancel = () => {
	isShowDialog.value = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = ruleForm.value;
			if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == '' || ruleForm.value.id == 0) {
				await addEmployee(values);
			} else {
				await updateEmployee(values);
			}
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: 'error',
			});
		}
	});
};

const projectProjectIdDropdownList = ref<any>([]);
const getProjectProjectIdDropdownList = async () => {
	let list = await getProjectProjectIdDropdown();
	projectProjectIdDropdownList.value = list.data.result ?? [];
};
getProjectProjectIdDropdownList();

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
